package com.xx.eshop.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xx.eshop.entity.Attr;
import com.xx.eshop.entity.Category;
import com.xx.eshop.entity.Product;
import com.xx.eshop.service.AttrService;
import com.xx.eshop.service.CategoryService;
import com.xx.eshop.service.ProductService;

@Controller
public class Product2Controller {
	
	@Autowired
	private CategoryService categoryService;
	
	@Autowired
	private AttrService attrService;
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping("/demo/findAllCategory")
	@ResponseBody
	public List<Category> findAllCategory() {
		return categoryService.findAll();
	}
	
	@RequestMapping("/demo/findAllAttrByCid")
	@ResponseBody
	public List<Attr> findAllAttrByCid(int cid){
		return attrService.findByCategoryFetch(cid);
	}
	
	@RequestMapping("/demo/save")
	@ResponseBody
	public Map<String,Object> save(Product product,
			@RequestParam List<String> attr_names,
			@RequestParam List<String> attr_values,
			@RequestParam List<String> images) {
		System.out.println(product);
		System.out.println(attr_names);
		System.out.println(attr_values);
		System.out.println(images);
		
		productService.save(product, attr_names, attr_values, images);
		
		Map<String,Object> map = new HashMap<>();
		map.put("success", true);
		return map;
	}

}
